<?php
/**
 * 重庆柯一网络有限公司
 * 遵循MT协议，开源并可商业使用，没有任何限制
 * @Author:cqkyi
 * @Date: 2020/9/13 19:00
 * 官方惟一地址：www.cqkyi.com
 */

namespace app\common\model\admin;


use app\common\utils\Captcha;
use app\common\utils\Res;
use app\common\validate\LoginValidate;
use app\common\validate\UserValidate;
use think\Model;

class UserModel extends Model
{

   protected $name="sys_user";

   protected $pk="user_id";


    /**
     * @param $username 用户名称
     * @return UserModel 返回用户信息
     */
   public static function findByUserName($username)
   {
       return self::where('username',$username)->find();
   }


    /**
     * @param $param 接收的参数
     * @return UserModel|\think\response\Json
     */
   public static function userLogin($param)
   {

       $validate = new LoginValidate();
       if(!$validate->scene('api')->check($param)){
         return Res::ModelMsg($validate->getError());
       }
       if(!app()->make(Captcha::class)->check($param['code'])){
           return Res::ModelMsg("验证码错误");
       }
       $result = self::findByUserName($param['username']);
       if(empty($result)){
           return Res::ModelMsg("管理员不存在");
       }
       if(md5($param['password'])!=$result['password']){
           return Res::ModelMsg('密码错误');
       }
       if(1 != $result['status']){
           return Res::ModelMsg('账号被禁用');
       }
       return $result;
   }

    public static function pageMap($map,$offset,$limit){
        return self::where($map)->limit($offset, $limit)->order('user_id desc')->select();
    }

    public static function pageMapCount($map){
        return self::where($map)->count();
    }


    public static function add($param,$ruls)
    {
        try {
            $validate = new UserValidate();
            if (!$validate->check($param)) {
                return Res::ModelMsg($validate->getError());
            }
            $userModel = new UserModel();
            $userModel->save($param);
            $userRoleModel = new UserRoleModel();
            foreach ($ruls as $k=>$v){
                $data=[
                    'user_id'=>$userModel->user_id,
                    'role_id'=>$v
                ];

                $userRoleModel->insert($data);
            }
            return  Res::Modelok("添加成功");
        }catch (\PDOException $exception){
            return Res::ModelMsg($exception->getMessage());
        }
    }

    public static function edit($param,$ruls)
    {
        try {
            $result = self::find($param['user_id']);
            $validate = new UserValidate();
            if (!$validate->scene('api')->check($param)) {
                return Res::ModelMsg($validate->getError());
            }
            if($param['password']==$result['password']){
                $param['password']=$param['password'];
            }else{
                $param['password']=md5($param['password']);
            }
           self::update($param, ['user_id' => $param['user_id']]);
            UserRoleModel::delUserId($param['user_id']);
            $userRoleModel = new UserRoleModel();
            foreach ($ruls as $k=>$v){
                $data=[
                    'user_id'=>$param['user_id'],
                    'role_id'=>$v
                ];

                $userRoleModel->insert($data);
            }
            return  Res::Modelok("修改成功");
        }catch (\PDOException $exception){
            return Res::ModelMsg($exception->getMessage());
        }
    }


    public static function del($id)
    {
        try{

            $res=self::find($id);
            if($id==1){
                return Res::ModelMsg('超级管理员不允许删除');
            }
            if(empty($res)){
                return Res::ModelMsg('你要删除的内容不存在');
            }
            self::destroy($id);
            UserRoleModel::delUserId($id);
            return Res::Modelok('删除成功');
        }catch (PDOException $e){
            return Res::ModelMsg($e->getMessage());
        }
    }


    public static function batchRemove($param)
    {
        try {
            if(in_array(1,$param)){
                return Res::ModelMsg("超级管理员不允许删除");
            }
            foreach ($param as $k=>$v){
                UserRoleModel::delUserId($v);

            }
            self::destroy($param);
            return Res::Modelok( '删除成功');
        }catch(PDOException $e){
            return Res::ModelMsg( $e->getMessage());
        }
    }
}